
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章新增</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <!-- jq -->
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/layui/layui.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章新增
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control title" id="inputTitle" placeholder="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img id="coverImg" src="./images/2.jpg" class="article_cover">
                        <input name="cover" type="file" id="inputCover" onchange="changeImg()">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCategory" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select id="category" class="form-control category" name="categoryId">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <input id="articleDate" name='date' class="jeinput" type="text" placeholder="请选择发布日期"  readonly>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <!-- 富文本编辑区域 -->
                        <textarea id="articleContent" placeholder="请输入文章内容"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button id="save_p" type="submit" class="btn btn-success btn-edit" >发布</button>
                        <button id="save_c" type="submit" class="btn btn-default btn-draft">存为草稿</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="./js/http-myajax.js"></script>
    <script src="./libs/tinymce_5.5.1/tinymce.min.js"></script>
    <script src="./libs/art-template/template-web.js"></script>
    <!-- 定义文章分类模板 -->
    <script id="art_cate" type="text/html">
     <!-- 遍历数据 -->
     {{each data}}
     <option value="{{$value.id}}">{{$value.name}}</option>
     {{/each}}
    </script>
    <script>
        $(function() {
            get_cate_list();
            date_render();
            init_taxtarea();
            
            //给发布注册点击事件
            $('#save_p').click(function(e) {
                e.preventDefault();
                save('已发布') 
            })
            //给草稿注册点击事件
            $('save_c').click(function(e) {
                e.preventDefault();
                save('') ;
            })
        })
        //获取分类下单数据
        function get_cate_list() {
            myAjax('get','/admin/category/list',null,function(resData) {
                /* 
                 resData格式：{
                                "code": 200,
                                "msg": "获取成功",
                                "data": [{
                                    "id": 1,
                                    "name": "爱生活",
                                    "slug": "热爱生活"
                                }]
                            }
                */
                let resHtml = template('art_cate',resData);
                $('#category').html(resHtml)
            })
        }
        function date_render() {
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                //  渲染日期面板到 id=articleDate 的文本框中
                laydate.render({
                    elem: '#articleDate' //指定元素
                });

            });
        }
        function init_taxtarea() {
            tinymce.init({
                selector: '#articleContent',
                language:'zh_CN' // 通过这个就会自动找到 tinymce_5.5.1下面的langs中的zh_CN.js
            });
        }
        function changeImg() {
            let file = document.querySelector('#inputCover').files[0];
            //将文件生成一个本地可以访问的路径
            let filePath = URL.createObjectURL(file);
            //将本地的路径设置给img的src
            $('#coverImg').attr('src',filePath)
        }
        //实现文章发布功能
        function save(type) {
            // 收集页面输入的数据
            let $title = $('#inputTitle').val();
            let cover = document.querySelector('#inputCover').files[0];
            let $categoryId = $('#category').val();
            let $date = $('#articleDate').val();
            let content = tinyMCE.editors['articleContent'].getContent();
            let state = type;
            // 将数据包装成formData类型
            let formData = new FormData();
            formData.append('title',$title)
            formData.append('cover',cover)
            formData.append('categoryId',$categoryId)
            formData.append('date',$date)
            formData.append('content',content)
            formData.append('state',state)
            //发送ajax请求将formData数据提交
            myAjax('POST','/admin/article/publish',formData,function(resData) {
                if(resData.code !== 200) {
                    alert(resData.msg);
                    return;
                }
                window.location = './article_list.html';
            })
        }
    </script>
   
</body>
</html>

